package cn.kgc.controller;

import cn.kgc.pojo.Disburse;

import cn.kgc.service.DisburseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李梦煊
 * @version 1.0
 * @date 2022/10/14 - 16:55
 */
@Controller
public class DisburseController {
    @Autowired
    private DisburseService disburseService;

    //查询支出
    @RequestMapping("/getAllDisbure")
    @ResponseBody
    public Map<String, Object> getAllDisbure(String page, String limit,String clientName){
        HashMap<String, Object> map = new HashMap<>();
        int crrPage = 1;
        if (page != null && !"".equals(page)) {
            crrPage = Integer.parseInt(page);
        }
        int lim = Integer.parseInt(limit);
        crrPage = (crrPage - 1) * lim;
        List<Disburse> allDisbure =disburseService.getAllDisbure(crrPage, lim,clientName);

        //得到总页数
        int totalPage = disburseService.getTotalCount(clientName);
        map.put("count", totalPage);
        map.put("data", allDisbure);
        map.put("code", 0);
        map.put("msg", "成功获取到图表");
        return map;
    }


    //饼图
    @RequestMapping("/getDisByName")
    @ResponseBody
    public Map<String, Object> getDisByName(){
        HashMap<String, Object> map = new HashMap<>();

        List<Map<Integer,String>> count = disburseService.count();

        map.put("count",count);
        map.put("code", 0);
        map.put("msg", "成功获取到图表");
        return map;
    }

    //报表
    @RequestMapping("/getColumn")
    @ResponseBody
    public Map<String, Object> getColumn(){
        HashMap<String, Object> maps = new HashMap<>();

        List<Map<Integer,String>> column = disburseService.column();

        maps.put("column",column);
        maps.put("codes", 0);
        maps.put("msgs", "成功获取到图表");
        return maps;
    }



    //新增
    @RequestMapping("/addDisburse")
    @ResponseBody
    public String addDisburse(Disburse disburse) {

/*        disburse.setRoomId(disburse.getRoomId());

        disburse.setOrderId(disburse.getOrderId());
        disburse.setPaidTime(disburse.getPaidTime());
        disburse.setClientId(disburse.getClientId());*/
        disburseService.addDisburse(disburse);
        return "getAllDisbure";
    }

    //修改
    @RequestMapping("/updateDisById")
    @ResponseBody
    public String updateDisById(Disburse disburse) {

        disburse.getPaidId();
        disburse.setCause(disburse.getCause());
        disburse.setUtilities(disburse.getUtilities());


        disburseService.updateDisById(disburse);
        return "getAllDisbure";
    }

    //删除
    @RequestMapping("/delDisById")
    @ResponseBody
    public String delDisById(String paidId){
        disburseService.delDisById(paidId);
        return "getAllDisbure";
    }
}
